package cn.neusoft.controller;

import cn.neusoft.domain.Dept;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.DeptQuery;
import cn.neusoft.service.DeptService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import cn.neusoft.vo.TreeSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/dept", produces = "application/json;charset=utf-8")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DeptController {
    @Autowired
    DeptService deptService;

    @PostMapping
    @ApiOperation(value = "新增部门")
    public ResultBean add(@RequestBody Dept dept) {

        if (dept == null
                || StringUtils.isEmpty(dept.getDept_name())
                || dept.getParent_id() == null || dept.getParent_id() == 0
                || StringUtils.isEmpty(dept.getStatus())
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        deptService.add(dept);

        return ResultBean.success();
    }

    @PutMapping
    @ApiOperation(value = "修改部门信息")
    public ResultBean update(@RequestBody Dept dept) {

        if (dept == null
                || dept.getDept_id() == null || dept.getDept_id() == 0
                || StringUtils.isEmpty(dept.getDept_name())
                || dept.getParent_id() == null || dept.getParent_id() == 0
                || StringUtils.isEmpty(dept.getStatus())
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        deptService.update(dept);

        return ResultBean.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除部门")
    public ResultBean deleteById(@PathVariable("id") Long dept_id) {

        if (dept_id == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        deptService.deleteById(dept_id);

        return ResultBean.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门信息")
    public ResultBean findById(@PathVariable("id") Long dept_id) {

        if (dept_id == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Dept dept = deptService.findById(dept_id);

        return ResultBean.success(dept);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有部门的信息")
    public ResultBean findAll() {
        List<Dept> deptList = deptService.findAll();

        return ResultBean.success(deptList);
    }

    @PostMapping("/page")
    @ApiOperation("分页模糊查询部门信息")
    public ResultBean findByPage(@RequestBody DeptQuery deptQuery) {

        if(deptQuery == null
                || StringUtils.isEmpty(deptQuery.getPageNow())
                || StringUtils.isEmpty(deptQuery.getPageSize())
        ){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        PageBean<Dept> pageBean = deptService.findByPage(deptQuery);

        return ResultBean.success(pageBean);
    }

    @GetMapping("/deptTreeList")
    @ApiOperation("得到部门树列表")
    public ResultBean getDeptTreeList() {

        List<Dept> deptTreeList = deptService.getDeptTreeList();

        return ResultBean.success(deptTreeList);
    }

    @GetMapping("/normalDeptTreeSelect")
    @ApiOperation("得到正常部门的级联下拉列表")
    public ResultBean getNormalDeptTreeSelect() {

        List<TreeSelect> normalDeptTreeSelect = deptService.getNormalDeptTreeSelect();

        return ResultBean.success(normalDeptTreeSelect);
    }

    @GetMapping("/normalSubstationTreeSelect")
    @ApiOperation("得到正常分站的级联下拉列表")
    public ResultBean getNormalSubstationTreeSelect() {

        List<TreeSelect> normalSubstationTreeSelect = deptService.getNormalSubstationTreeSelect();

        return ResultBean.success(normalSubstationTreeSelect);
    }

    @GetMapping("/normalWarehouseTreeSelect")
    @ApiOperation("得到某一分站的正常仓库的级联下拉列表")
    public ResultBean getNormalWarehouseTreeSelect() {

        List<TreeSelect> normalWarehouseTreeSelect = deptService.getNormalSubWarehouseTreeSelect();

        return ResultBean.success(normalWarehouseTreeSelect);
    }


//    @GetMapping("/allSubstations")
//    @ApiOperation("得到所有分站")
//    public ResultBean getAllSubstations() {
//
//        List<Dept> allSubstations = deptService.getAllSubstations();
//
//        return ResultBean.success(allSubstations);
//    }
//
//    @GetMapping("/allWarehouses")
//    @ApiOperation("得到所有仓库")
//    public ResultBean getAllWarehouses() {
//
//        List<Dept> allWarehouses = deptService.getAllWarehouses();
//
//        return ResultBean.success(allWarehouses);
//    }


}
